package com.hongkzh.www.look.view.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RadioButton;

import com.hongkzh.www.R;
import com.hongkzh.www.look.enterprise.userinfo.model.bean.UserInfo;
import com.hongkzh.www.other.utils.SharedPreferencesUtil;
import com.hongkzh.www.other.utils.UIUtils;
import com.hongkzh.www.look.enterprise.presenter.MainPresenter;
import com.hongkzh.www.view.activity.BaseActivity;
import com.hongkzh.www.view.fragment.LGFragment;
import com.hongkzh.www.view.fragment.LIFragment;
import com.hongkzh.www.look.view.framgent.LKFragment;
import com.hongkzh.www.view.fragment.LXFragment;
import com.hongkzh.www.view.fragment.LYFragment;
import com.hongkzh.www.look.view.iview.MainIView;

/**
 * 首页
 */
public class MainActivity extends BaseActivity<MainPresenter> implements MainIView, View.OnClickListener {

    private RadioButton lk;
    private RadioButton ly;
    private RadioButton lg;
    private RadioButton lx;
    private RadioButton li;
    private LKFragment lkFragment;
    private LYFragment lyFragment;
    private LGFragment lgFragment;
    private LXFragment lxFragment;
    private LIFragment liFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setTranslucent(R.color.color_D8D8D8);
        UserInfo userInfo = new UserInfo();
        userInfo.setIsLogin(true);
        userInfo.setLoginUid("1");
        new SharedPreferencesUtil(UIUtils.getContext()).saveUserInfo(userInfo);

        setPresenter(new MainPresenter());
        initDefultView();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void init() {
        lk = findViewById(R.id.main_lk);
        ly = findViewById(R.id.main_ly);
        lg = findViewById(R.id.main_lg);
        lx = findViewById(R.id.main_lx);
        li = findViewById(R.id.main_li);
    }

    @Override
    protected void setListener() {
        lk.setOnClickListener(this);
        ly.setOnClickListener(this);
        lg.setOnClickListener(this);
        lx.setOnClickListener(this);
        li.setOnClickListener(this);
    }

    //初始化视图
    private void initDefultView() {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        lkFragment = new LKFragment();
        ft.add(R.id.main_fl, lkFragment);
        ft.commit();
    }

    //点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_lk:
                select(0);
                break;
            case R.id.main_ly:
                select(1);
                break;
            case R.id.main_lg:
                select(2);
                break;
            case R.id.main_lx:
                select(3);
                break;
            case R.id.main_li:
                select(4);
                break;
        }
    }

    //点击切换fragment,如果fragment为空，则创建，如果不为空，就显示
    private void select(int i) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        hidtFragment(ft);
        switch (i) {
            case 0:
                if (lkFragment == null) {
                    lkFragment = new LKFragment();
                    ft.add(R.id.main_fl, lkFragment);
                } else {
                    ft.show(lkFragment);
                }
                break;
            case 1:
                if (lyFragment == null) {
                    lyFragment = new LYFragment();
                    ft.add(R.id.main_fl, lyFragment);
                } else {
                    ft.show(lyFragment);
                }
                break;
            case 2:
                if (lgFragment == null) {
                    lgFragment = new LGFragment();
                    ft.add(R.id.main_fl, lgFragment);
                } else {
                    ft.show(lgFragment);
                }
                break;
            case 3:
                if (lxFragment == null) {
                    lxFragment = new LXFragment();
                    ft.add(R.id.main_fl, lxFragment);
                } else {
                    ft.show(lxFragment);
                }
                break;
            case 4:
                if (liFragment == null) {
                    liFragment = new LIFragment();
                    ft.add(R.id.main_fl, liFragment);
                } else {
                    ft.show(liFragment);
                }
                break;
        }
        ft.commit();
    }

    //隐藏Fragment的方法，先判断fragment是否为空，如果不为空则隐藏Fragment
    private void hidtFragment(FragmentTransaction fragmentTransaction) {
        if (lkFragment != null) {
            fragmentTransaction.hide(lkFragment);
        }
        if (lyFragment != null) {
            fragmentTransaction.hide(lyFragment);
        }
        if (lgFragment != null) {
            fragmentTransaction.hide(lgFragment);
        }
        if (lxFragment != null) {
            fragmentTransaction.hide(lxFragment);
        }
        if (liFragment != null) {
            fragmentTransaction.hide(liFragment);
        }
    }

    @Override
    public void onError(Exception e) {

    }
}
